import {
	publicRoutes,
	privateRoutes
} from '@/router'

export default {
	namespaced: true,

	state: () => ({
		// 路由表：初始时所拥有的路由表
		routes: publicRoutes
	}),

	mutations: {
		// 增加路由
		setRoutes(state, newRoutes) {
			state.routes = [...publicRoutes, ...newRoutes]
		}
	},

	actions: {
		// 根据权限数据筛选路由
		// menus 权限数据
		filterRoutes(context, menus) {
			// 筛选之后需要进行添加的路由表数组
			const routes = []

			menus.forEach(key => {
				routes.push(...privateRoutes.filter(item => item.name === key))
			})

			routes.push({
				// 所有不匹配的路由，全部进入404
				path: '/:catchAll(.*)',
				redirect: '/404'
			})

			context.commit('setRoutes', routes)

			return routes
		}
	}
}
